package cn.chiship.framework.upms.biz.system.service.impl;

import cn.chiship.framework.common.service.ExportService;
import cn.chiship.framework.common.util.FrameworkUtil2;
import cn.chiship.framework.upms.biz.system.entity.UpmsProof;
import cn.chiship.framework.upms.biz.system.entity.UpmsProofExample;
import cn.chiship.framework.upms.biz.system.mapper.UpmsProofMapper;
import cn.chiship.framework.upms.biz.system.service.UpmsProofService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.encryption.ProofUtil;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通行票据业务接口实现层
 * 2021/9/27
 *
 * @author lijian
 */
@Service
public class UpmsProofServiceImpl extends BaseServiceImpl<UpmsProof, UpmsProofExample> implements UpmsProofService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsProofServiceImpl.class);

    @Resource
    UpmsProofMapper upmsProofMapper;
    @Resource
    ExportService exportService;


    @Override
    public BaseResult insertSelective(UpmsProof upmsProof) {
        JSONObject proofJson = ProofUtil.generateProof();
        upmsProof.setAppId(proofJson.getString("appId"));
        upmsProof.setAppKey(proofJson.getString("appKey"));
        upmsProof.setAppSecret(proofJson.getString("appSecret"));
        upmsProof.setSignKey(proofJson.getString("signKey"));
        upmsProof.setPrivateKey(proofJson.getString("privateKey"));
        upmsProof.setPublicKey(proofJson.getString("publicKey"));
        return super.insertSelective(upmsProof);
    }

    @Override
    public void exportData(HttpServletResponse response, Map<String, Object> paramMap) throws Exception {
        String fileName = "用户密钥";
        String sheetName = "用户密钥";
        String sheetTitle = "用户密钥示例";
        ArrayList<String> labels = new ArrayList<>();
        labels.add("系统识别码");
        labels.add("项目名称");
        labels.add("项目描述");
        labels.add("AppKey");
        labels.add("AppSecret");
        labels.add("AppId");
        labels.add("签名串");
        labels.add("公钥");
        labels.add("私钥");
        UpmsProofExample upmsProofExample = new UpmsProofExample();
        UpmsProofExample.Criteria criteria = upmsProofExample.createCriteria().andIsDeletedEqualTo(BaseConstants.NO);
        Object projectName = paramMap.get("projectName");
        if (!StringUtil.isNull(projectName)) {
            criteria.andProjectNameLike(projectName + "%");
        }


        List<UpmsProof> upmsProofs = upmsProofMapper.selectByExample(upmsProofExample);

        List<List<String>> valueList = new ArrayList<>();
        for (UpmsProof item : upmsProofs) {
            List<String> values = new ArrayList<>();
            values.add(FrameworkUtil2.getGlobalProperties().getId());
            values.add(item.getProjectName());
            values.add(item.getProjectDesc());
            values.add(item.getAppKey());
            values.add(item.getAppSecret());
            values.add(item.getAppId());
            values.add(item.getSignKey());
            values.add(item.getPublicKey());
            values.add(item.getPrivateKey());
            valueList.add(values);
        }
        exportService.exportByType(response, paramMap.get("exportType").toString(), fileName, sheetName, sheetTitle, labels, valueList);
    }

}
